using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Jasongozy.Copor {
    public abstract class AbstractFunction : IFunction {
       
         
        #region IFunction Members

        public abstract string FuncName {
            get;
        }
        private Dictionary<string, string> _Property;
        public Dictionary<string, string> Property {
            get {
                return _Property;
            }
            set {
                _Property = value;
            }
        }

        public virtual void IniteProperty(Dictionary<string, string> input) {
            _Property = new Dictionary<string, string>();
            foreach (KeyValuePair<string, string> item in input) {
                _Property[item.Key] = item.Value;
            }
        }

        public abstract string Execute();
        
       
        #endregion

        protected string GetParamName(string input) {
            Match m = RegexMatch.TEMPLATE_PARAM_MATCH.Match(input);
            if (!m.Success) {
                m = RegexMatch.FUNC_VAR.Match(input);
                if (!m.Success) {
                    m = RegexMatch.TEMPLATE_PARAM_MATCH.Match(input);
                    if (!m.Success) return "";
                }
            }
            return m.Groups[1].Value;
        }

        protected virtual void CheckInput() {
        }

        protected virtual void InnerReplace() {
            if(!Property.ContainsKey("content")) return;
            string content =  Property["content"];
            foreach (string key in Property.Keys) {
                if (key != "content") {
                    content = TemplateParamInjection.ReplaceFuncPro(content, Property);
                }
            }
            Property["content"] = content;
        }
    }
}
